package com.exam.sacrifice.ui;

import static android.view.View.VISIBLE;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.exam.commonbiz.base.BaseActivity;
import com.exam.commonbiz.base.BaseViewModel;
import com.exam.commonbiz.util.OnFastClickListener;
import com.exam.commonbiz.util.ToastUtil;
import com.exam.sacrifice.R;
import com.exam.sacrifice.databinding.ActivityLiuLiShiJieBinding;
import com.exam.sacrifice.util.BitmapCacheUtils;
import com.exam.sacrifice.widget.CustomPopupWindow;

import java.util.Random;

public class LiuLiShiJieActivity extends BaseActivity<BaseViewModel, ActivityLiuLiShiJieBinding> {

    private boolean isHeHuaPosition = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_liu_li_shi_jie;
    }

    @Override
    public void initData() {

        initHeHuaJinChangVideo();
        binding.include.tvTitle.setText("琉璃世界");
        binding.include.tvTitle.setTextColor(ContextCompat.getColor(context, R.color.white));
        binding.include.ivback.setImageDrawable(ContextCompat.getDrawable(context, com.exam.commonbiz.R.drawable.back_white));
        binding.include.ivback.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                back();
            }
        });

        initLianHua();
    }

    private void initLianHua() {

        String[] beiwenArr = new String[5];
        beiwenArr[0] = "公元二零二三年岁次癸卯，朔日甲子，孝子贤孙谨以清酌庶馐之奠，致祭于显考（妣）XXX老大人（孺人）之灵前曰：\n" +
                "呜呼！吾父（母）八秩高龄，德配坤贞。\n一生勤俭，睦族和邻。教子有方，耕读传家。\n含辛茹苦，养育恩深。四世同堂，福寿双全。\n今虽驾鹤，懿范长存。音容宛在，德泽永铭。\n哀哉痛哉，伏惟尚飨！";
        beiwenArr[1] = "青山有幸埋忠骨,绿水无声泣鬼神。\n千秋伟业铭心间,万古流芳颂德仁。\n岁月悠悠人已去,英名昭昭永长存。";
        beiwenArr[2] = "父爱如山岳般巍峨, 深沉且恒久, 为我等后辈树立楷模。\n他的教诲, 似阳光普照, 驱散迷雾, 指引我前行路。";
        beiwenArr[3] = "吾父英勇一世，坚韧不拔。\n为家之栋梁，为子之楷模。\n一生勤勉，兢兢业业。\n今父仙逝，吾辈哀痛不已。\n愿父安息，灵魂永存。";
        beiwenArr[4] = "一生勤勉，德润乡邻；\n" +
                "教子持家，仁爱传馨。\n" +
                "音容笑貌，犹在眼前；\n" +
                "厚德遗风，永泽后昆。";
        CustomPopupWindow popupWindow = new CustomPopupWindow(this, R.layout.dialog_layout_renwujianjie);
        View contentView = popupWindow.getContentView();
        ImageView ivAvatar = contentView.findViewById(R.id.iv_avatar);
        TextView tvContent = contentView.findViewById(R.id.tv_content);
        ivAvatar.setAlpha(0.8f);
        tvContent.setAlpha(0.5f);

//        Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.jisixiangkuang);
//        Bitmap bitmap = BitmapCacheUtils.applyCenterFocusEffect(bmp);
//        ivAvatar.setImageBitmap(bitmap);
        binding.rl1.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                tvContent.setText(beiwenArr[new Random().nextInt(5)]);
                popupWindow.showAbove(binding.rl1);
            }
        });
        binding.rl2.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                tvContent.setText(beiwenArr[new Random().nextInt(5)]);
                popupWindow.showAbove(binding.rl2);
            }
        });
        binding.rl3.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                tvContent.setText(beiwenArr[new Random().nextInt(5)]);
                popupWindow.showAbove(binding.rl3);
            }
        });
        binding.rl4.setOnClickListener(new OnFastClickListener() {
            @Override
            public void onFastClick(View v) {
                tvContent.setText(beiwenArr[new Random().nextInt(5)]);
                popupWindow.showAbove(binding.rl4);
            }
        });

    }

    private void initHeHuaJinChangVideo() {

        // 设置视频源 (res/raw/sample.mp4)
        Uri videoUri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.hehua_jinchang);
        binding.videoViewHeHuaJinChang.setVideoURI(videoUri);

        // 设置监听器
        binding.videoViewHeHuaJinChang.setOnPreparedListener(mp -> {
            binding.videoViewHeHuaJinChang.start();
        });

        binding.videoViewHeHuaJinChang.setOnCompletionListener(mp -> {
            binding.rlLianhua.setVisibility(VISIBLE);
            if (!isHeHuaPosition) {
                Uri videoUri2 = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.hehua);
                binding.videoViewHeHuaJinChang.setVideoURI(videoUri2);
            }
            binding.videoViewHeHuaJinChang.start();
            binding.rlMenu.setVisibility(VISIBLE);
            isHeHuaPosition = true;
        });

        binding.videoViewHeHuaJinChang.setOnErrorListener((mp, what, extra) -> {
            ToastUtil.getInstance().showToast("播放错误");
            return true;
        });
    }

    @Override
    public void back() {
        if (binding.videoViewHeHuaJinChang.isPlaying()) {
            binding.videoViewHeHuaJinChang.stopPlayback();
        }
        finish();
    }
}